import { staticDir } from "../utils/storage-utils";
import logger from "../utils/logger-utils";
import * as createError from "http-errors"
import * as express from "express"
import * as multer from "multer"
import * as cors from "cors"

// 引入路由模块
import userRouter from '../routers/user_router'
import videoProjectRouter from '../routers/video_project_router'


const webService = express()

// 开放跨域请求
webService.use(cors());

// 配置中间件
// for parsing application/json
webService.use(express.json());
// for parsing application/x-www-form-urlencoded
webService.use(express.urlencoded({ extended: true }));
// for parsing multipart/form-data 这个位置会覆盖之后路由中的中间件
// webService.use(multer({ dest: uploadDir }).any());
// webService.use(multer().none());
// 配置静态资源路径
webService.use(express.static(staticDir));

// 注册路由模块
webService.get("/", (req, res) => {
  res.send("hello world 这是后端接口地址，请勿直接访问！")
});

webService.use("/user", userRouter);
webService.use("/videoProject", videoProjectRouter);


// 捕捉404并转发到错误处理器
webService.use(function (req, res, next) {
  logger.error(`404 || ${res.statusMessage} - ${req.originalUrl} - ${req.method} - ${req.ip}`);
  next(createError(404));
});

// 导出 webService 模块
export default webService;